package com.wms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.common.QueryPageParam;
import com.wms.common.Result;
import com.wms.entity.Goods;
import com.wms.service.GoodsService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * GoodsController 物品控制层
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

   @Autowired
    private GoodsService goodsService;

    /**
     * 方法名save 前端传来物品数据，通过该方法新增物品数据
     * @param goods
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Goods goods){

        boolean save = goodsService.save(goods);

        return save?Result.suc():Result.fail();
    }

    /**
     * 方法名update 前端传来物品数据，通过该方法更新物品数据
     * @param goods
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Goods goods){

        boolean update = goodsService.updateById(goods);

        return update?Result.suc():Result.fail();
    }

    /**
     * 方法名del 前端传来物品id，通过该方法删除指定物品
     * @param id
     * @return
     */
    @GetMapping("/del")
    public Result del(@RequestParam String id){

        boolean delete = goodsService.removeById(id);

        return delete?Result.suc():Result.fail();
    }

    /**
     * 方法名listPage 前端传来数据 包括当前页，条数，要查询的物品名数据，
     * 通过该方法查询物品数据并返回给前端
     * @param query
     * @return
     */
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        Page<Goods> page = new Page<>(query.getPageNum(), query.getPageSize());
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Goods::getName, query.getParam().get("name"));
        queryWrapper.like(Goods::getGoodsTypeId, query.getParam().get("goodsTypeId"));
        queryWrapper.like(Goods::getStorageId, query.getParam().get("storageId"));
        goodsService.pageCC(page, queryWrapper);
        return Result.suc(page, (long) page.getRecords().size());
    }
}
